package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.WareHouseService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.WareHouse;
import com.avantis.common.msg.YResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/1/2 7:30 PM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/

@RestController
@RequestMapping("/warehouse")
public class WareHouseController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WareHouse.class);

    @Autowired
    private WareHouseService wareHouseService;

    @PostMapping("/addwarehouse")
    @ApiOperation(value = "仓库添加接口", httpMethod = "POST", notes = "")
    public YResult AddWareHouse(@RequestBody @Validated WareHouse wareHouse){
       YResult yResult = restProcessor(() -> {
            boolean wH = wareHouseService.checkByWarehouseCode(wareHouse.getWarehouseCode());
            if (wH) {
                AdminUser adminUser = CurrentUser.get();
                wareHouse.setCreater(adminUser.getUsername());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                wareHouse.setCreatedon(new Date());
                wareHouse.setModifyon(new Date());
                wareHouseService.saveWarehouse(wareHouse);
                return YResult.ok();
            }else {
                return YResult.error("此仓库编号已经存在，无法添加仓库！");
            }
        });

       return yResult;
    }

    @GetMapping("/listwarehouse")
    @ApiOperation(value = "仓库列表接口", httpMethod = "GET", notes = "")
    public YResult ListWareHouse() {
        YResult yResult = restProcessor(() -> {
            try {
                HashMap<String, Object> hashMapwh = new HashMap<>();
                List<WareHouse> wareHouses = wareHouseService.findAll();
                hashMapwh.put("wareHouses", wareHouses);
                return YResult.ok(hashMapwh);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @GetMapping("/detailwarehouse/{warehouseid}")
    @ApiOperation(value = "仓库信息详情", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "warehouseid", value = "仓库ID", dataType = "int"))
    public  YResult DetailWareHouse(@PathVariable("warehouseid") Integer warehouseid) {
        YResult yResult = restProcessor(() -> {
            try {
                HashMap<String, Object> hashMap = new HashMap<>();
                WareHouse wareHouse = wareHouseService.findOne(warehouseid);
                hashMap.put("wareHouse", wareHouse);
                return YResult.ok(hashMap);
            }catch (Exception e){
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PutMapping("/editwarehouse/{warehouseid}")
    @ApiOperation(value = "编辑仓库信息", httpMethod = "PUT", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "warehouseid", value = "仓库ID", dataType = "int"))
    public YResult EditWareHouse(@PathVariable("warehouseid") String warehouseid,
                                 @RequestBody WareHouse wareHouse) {
        YResult yResult = restProcessor(() -> {
            try {
                AdminUser adminUser = CurrentUser.get();
                wareHouse.setModifier(adminUser.getUsername());
                wareHouse.setId(warehouseid);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                wareHouse.setModifyon(new Date());
                wareHouseService.saveWarehouse(wareHouse);
                return YResult.ok();
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @DeleteMapping("/deletewarehouse/{warehouseid}")
    @ApiOperation(value = "删除指定仓库", httpMethod = "DELETE", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "warehouseid", value = "仓库ID", dataType = "int"))
    public YResult DeleteWareHouse(@PathVariable("warehouseid") Integer warehouseid) {
        YResult yResult = restProcessor(() -> {
            try {
                wareHouseService.deleteById(warehouseid);
                return YResult.ok("删除仓库成功！");
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }
}
